
set matsize 5000
local repBS=100   /*number of bootstrap repetitions */


use "$project\merger_lbnr", clear
bysort lbnr: egen maxtemp=max(nworker)
keep if maxtemp>=50
drop if branche==25

tsset, clear
tsset lbnr year

replace target_full=F1.target_full
replace target_full=0 if target_full==. 

sort lbnr year
by lbnr: gen target_cumu=sum(target_full)
gen post_merger=(target_cumu==1)
gen ln_nworker=log(nworker)
by lbnr: egen ntarget=sum(target)
keep if ntarget<=1

/* Divide into quantiles based on firm FE of acquirer firms */
merge m:1 cvrnr year using "D:\Workdata\702525\users\Daniel\DecentralizationSorting\Alex\MA\merger_cvrnr", keep(1 3) keepus(cvrnr_merger merger_cumu) nogen
ren cvrnr_merger firmid
merge m:1 firmid using "D:\Workdata\702525\users\Daniel\DecentralizationSorting\Alex\MA\AKM\firmeffs_ma.dta", keep(1 3) keepus(fe_1) nogen
rename fe_1 fe_cvrnr

gen temp=1 if merger_cumu==0
bysort cvrnr: egen fe_cvrnr_pre=max(fe_cvrnr*temp)
gen temp2=1 if target_cumu==1
bysort lbnr: egen fe_cvrnr_premerger=max(fe_cvrnr_pre*temp2)
drop fe_cvrnr_pre
xtile fe_quantile_acquirer = fe_cvrnr_premerger, nq(2)
drop temp*

cap drop firmid
rename lbnr_merger firmid
merge m:1 firmid using "D:\Workdata\702525\users\Daniel\DecentralizationSorting\Alex\MA\AKM\firmeffs_ma_lbnr_separate.dta", keep(1 3) keepus(fe_1) nogen
rename fe_1 fe_lbnr_separate
xtile fe_quantile = fe_lbnr_separate, nq(2)


tsset lbnr year

/* Identify transitions */

gen ttarget=.
replace ttarget=1 if target_cumu==1&l.target_cumu==0&fe_quantile==4
replace ttarget=0 if target_cumu==0&l.target_cumu==0

/* Define lags of y */

gen y=ln_nworker
gen lag1y=l1.y
gen lag2y=l2.y
gen lag3y=l3.y
gen lag4y=l4.y

gen z=ln_hrwage
gen lag1z=l1.z
gen lag2z=l2.z
gen lag3z=l3.z
gen lag4z=l4.z

/* Drop observations with no data to estimate probit */

keep if lag1y!=.
keep if lag2y!=.
*keep if lag3y!=.
*keep if lag4y!=.
keep if lag1z!=.
keep if lag2z!=.
*keep if lag3z!=.

global lagy lag1y lag2y lag1z lag2z

/* Create lags of y */

gen ydep0=L4.y-L.y
gen ydep1=L3.y-L.y
gen ydep2=L2.y-L.y
gen ydep3=0

/* Create leads of y */

gen ydep4=y-L.y
gen ydep5=F1.y-L.y
gen ydep6=F2.y-L.y
gen ydep7=F3.y-L.y
gen ydep8=F4.y-L.y
gen ydep9=F5.y-L.y
gen ydep10=F6.y-L.y
gen ydep11=F7.y-L.y
gen ydep12=F8.y-L.y


/* Define sample */
keep if ttarget!=.

/* Set seed */
set seed 12345

/* Generate year by industry FE */
egen yearind=group(year branche)

****************************************************************************************
******DEFINES PROGRAM THAT COMPUTES REGRESSION ADJUSTMENT ESTIMATOR FOR ALL YEARS*******
***************************************************************************************
*************************************************************************************

capture program drop tefpas_ra
program define tefpas_ra, eclass

/* Creates year dummies and avoid using ones with no observation */
quietly: gen temp=ttarget if ydep0!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)

/* Estimates for year -4 */
cap: teffects ra (ydep0 $lagy dumyears*, noconstant) (ttarget), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.ttarget]
}
else{
matrix b=(.)
}
quietly drop dumyears* temp mtemp

/* Estimates for years -3 */

quietly: gen temp=ttarget if ydep1!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ra (ydep1 $lagy dumyears*, noconstant) (ttarget), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears* temp mtemp


/* Estimates for year -2 and -1 are 0 by construction */
matrix b=(b,0)
matrix b=(b,0)

/* Estimates for years 0 to 8 */

forvalues s=4(1)12 {
quietly: gen temp=ttarget if ydep`s'!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ra (ydep`s' $lagy dumyears*, noconstant) (ttarget), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears* temp mtemp
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end


*********************************************************************************************
***DEFINE PROGRAM THAT COMPUTES INVESRSE PROPENSITY SCORE REWEIGHTING ESTIMATOR FOR ALL YEARS***
********************************************************************************************
********************************************************************************************
capture program drop tefpas_ipw
program define tefpas_ipw, eclass

/* Create year dummies and avoids using ones with no observation */
quietly: gen temp=ttarget if ydep0!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)

/* Estimates for year -4 */
cap: teffects ipw (ydep0) (ttarget $lagy dumyears*, noconstant probit), atet iterate(7)

/* Save results */
if _rc==0{
matrix b=_b[ATET: r1vs0.ttarget]
}
else{
matrix b=(.)
}
quietly drop temp mtemp dumyears*

/* Estimates for year -3 to -2 */
forvalues s=1(1)2 {
quietly: gen temp=ttarget if ydep`s'!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ipw (ydep`s') (ttarget $lagy dumyears*, noconstant probit), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears* temp mtemp
}

/* Estimates for year -1 are 0 by construction */
matrix b=(b,0)

/* Estimates for years 0 to 8 */

forvalues s=4(1)12 {
quietly: gen temp=ttarget if ydep`s'!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ipw (ydep`s') (ttarget $lagy dumyears*, noconstant probit), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears*  temp mtemp
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end


*********************************************************************************************
***DEFINE PROGRAM THAT COMPUTES THE DOUBLY ROBUST ESTIMATOR FOR ALL YEARS*****************
********************************************************************************************
********************************************************************************************
capture program drop tefpas_ipwra
program define tefpas_ipwra, eclass

/* Creates year dummies and avoid using ones with no observation */
quietly: gen temp=ttarget if ydep0!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)

/* Estimates for year -4 */
cap: teffects ipwra (ydep0 $lagy dumyears*, noconstant) (ttarget), atet iterate(7)

/* Save the results */
if _rc==0{
matrix b=_b[ATET: r1vs0.ttarget]
}
else{
matrix b=(.)
}
quietly drop dumyears* temp mtemp

/* Estimates for years -3 */

quietly: gen temp=ttarget if ydep1!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ipwra (ydep1 $lagy dumyears*, noconstant) (ttarget), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears* temp mtemp


/* Estimates for year -2 and -1 are 0 by construction */
matrix b=(b,0)
matrix b=(b,0)

/* Estimates for years 0 to 8 */

forvalues s=4(1)12 {
quietly: gen temp=ttarget if ydep`s'!=.&ttarget!=.
quietly: bysort year branche: egen mtemp=max(temp)
quietly: tab yearind if mtemp==1, gen (dumyears)
cap: teffects ipwra (ydep`s' $lagy dumyears*, noconstant) (ttarget), atet iterate(7)
if _rc==0{
matrix b=(b, _b[ATET: r1vs0.ttarget])
}
else{
matrix b=(b,.)
}
quietly drop dumyears* temp mtemp
}

/* Post results in matrix b, variance computed by bootstrapping later */
ereturn post b
end



*********************************************************************************************
*********************************************************************************************
*****************************ESTIMATION PROCEDURES AND STORING RESULTS***********************
********************************************************************************************
********************************************************************************************

/* Bootstrap and save results*/


xtset, clear
bootstrap _b, reps(`repBS') cluster(lbnr): tefpas_ra
parmest, format (estimate min95 max95) saving ("$project\results\impulse_ra_q4", replace)


*********************************************************************************************
*****************************DRAW GRAPHS FOR IMPULSE RESPONSES******************************
********************************************************************************************
********************************************************************************************

/* Figure : local projections RA */
use "$project\results\impulse_ra_q1", clear
split parm, p("c")
keep if parm2!=""
destring parm2, force replace
gen time=parm2-5
tsset time

/* Plots figure */
twoway (tsline estimate, lcolor(black) lpattern(solid)) /*
*/ (tsline min95, lcolor(gray) lpattern(dash)) (tsline max95, lcolor(gray) lpattern(dash)), /* 
*/ ytitle("Change in employment log points") xtitle("Year around acquisition") /*
*/ legend(off) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) /*
*/ plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) xlabel(-4(2)8) ylabel(-0.2(0.1)0.1)















